using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace yW5Lib
{
	public class clsCharacters : clsGenericBase<clsCharacter>
	{
		public delegate void SavedEventHandler(int ID);

		private clsXml cXml;

		[CompilerGenerated]
		private SavedEventHandler SavedEvent;

		public clsCollection<clsCharacter> colChars => base.colObjects;

		public event SavedEventHandler Saved
		{
			[CompilerGenerated]
			add
			{
				SavedEventHandler savedEventHandler = SavedEvent;
				SavedEventHandler savedEventHandler2;
				do
				{
					savedEventHandler2 = savedEventHandler;
					SavedEventHandler value2 = (SavedEventHandler)Delegate.Combine(savedEventHandler2, value);
					savedEventHandler = Interlocked.CompareExchange(ref SavedEvent, value2, savedEventHandler2);
				}
				while ((object)savedEventHandler != savedEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				SavedEventHandler savedEventHandler = SavedEvent;
				SavedEventHandler savedEventHandler2;
				do
				{
					savedEventHandler2 = savedEventHandler;
					SavedEventHandler value2 = (SavedEventHandler)Delegate.Remove(savedEventHandler2, value);
					savedEventHandler = Interlocked.CompareExchange(ref SavedEvent, value2, savedEventHandler2);
				}
				while ((object)savedEventHandler != savedEventHandler2);
			}
		}

		public clsCharacters()
		{
			cXml = new clsXml();
		}

		public clsCharacter CharByID(int ID)
		{
			return base.get_ByID(ID);
		}

		public clsCharacter CharByIndex(int Index)
		{
			return base.get_ByIndex(Index);
		}

		public bool AddChar(ref clsCharacter Character)
		{
			if (Added(ref Character))
			{
				Character.Saved += Char_Saved;
				Character.AddedToProject = true;
				return true;
			}
			return false;
		}

		public void RemoveCharByID(int ID)
		{
			if (IDUsed(ID))
			{
				CharByID(ID).Saved -= Char_Saved;
				RemoveByID(ID);
			}
			else
			{
				clsShared.cLog.AddAction("Tried to remove character ID " + Conversions.ToString(ID) + " but it's not in the project.");
			}
		}

		public void RemoveCharByIndex(int Index)
		{
			try
			{
				CharByIndex(Index).Saved -= Char_Saved;
				RemoveByIndex(Index);
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		public string SelectedCharsAsXML(clsCollection<int> colIDS)
		{
			string result = "";
			StringBuilder stringBuilder = new StringBuilder();
			try
			{
				stringBuilder.AppendLine("\t<CHARACTERS>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsCharacter clsCharacter = (clsCharacter)enumerator.Current;
						if (colIDS.Contains(Conversions.ToString(clsCharacter.ID)))
						{
							stringBuilder.AppendLine(clsCharacter.DataAsXML());
						}
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				stringBuilder.AppendLine("\t</CHARACTERS>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool WriteXML(ref StringBuilder SBP)
		{
			try
			{
				bool result = true;
				SBP.AppendLine("\t<CHARACTERS>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsCharacter clsCharacter = (clsCharacter)enumerator.Current;
						SBP.AppendLine(clsCharacter.DataAsXML());
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				SBP.AppendLine("\t</CHARACTERS>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				bool result = false;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool CreateFromXML(string XMLData)
		{
			Clear();
			return UpdateFromXML(XMLData, ResetID: false, TriggerSaveEvent: false);
		}

		public bool UpdateFromXML(string XMLData, bool ResetID, bool TriggerSaveEvent)
		{
			bool result = true;
			try
			{
				string[] DataArray = new string[2];
				cXml.ExtractXMLArray(XMLData, "<CHARACTERS>", "</CHARACTERS>", "</CHARACTER>", ref DataArray);
				int num = DataArray.Length - 1;
				for (int i = 0; i <= num; i++)
				{
					if (Strings.InStr(1, DataArray[i], "<Character>", CompareMethod.Text) > 0)
					{
						clsCharacter Character = new clsCharacter();
						Character.CreateFromXML(DataArray[i]);
						if (Character.SortOrder == 0)
						{
							Character.SortOrder = i + 1;
						}
						if (ResetID)
						{
							Character.ID = 0;
							Character.SortOrder = base.Count + 1;
						}
						if (!AddChar(ref Character))
						{
							clsShared.cLog.AddAction("Unable to add character " + Character.Title + " to project.");
							return false;
						}
						if (TriggerSaveEvent)
						{
							Character.TriggerSaveEvent();
						}
					}
				}
				DataArray = null;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				result = false;
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool Resort()
		{
			if (base.Count <= 1)
			{
				return true;
			}
			bool result = default(bool);
			try
			{
				bool flag;
				int i;
				do
				{
					flag = true;
					int num = base.Count - 1;
					for (i = 1; i <= num; i++)
					{
						clsCharacter clsCharacter = base.get_ByIndex(i);
						clsCharacter cObject = base.get_ByIndex(i + 1);
						if (cObject.SortOrder < clsCharacter.SortOrder)
						{
							flag = false;
							RemoveByID(cObject.ID);
							if (!Inserted(ref cObject, i))
							{
								AddChar(ref cObject);
								clsShared.cLog.AddAction("Error while sorting characters - could not put " + cObject.Title + " back.");
								return result;
							}
						}
					}
				}
				while (!flag);
				i = 0;
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsCharacter clsCharacter = (clsCharacter)enumerator.Current;
						i = (clsCharacter.SortOrder = i + 1);
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		private void Char_Saved(int ID)
		{
			SavedEvent?.Invoke(ID);
		}

		public bool AlphaSortMajorMinor()
		{
			bool result = false;
			if (base.Count <= 1)
			{
				return true;
			}
			try
			{
				if (!AlphaSortHidden())
				{
					return result;
				}
				int num = base.Count + 1;
				int count = base.Count;
				for (int i = 1; i <= count; i++)
				{
					if (!base.get_ByIndex(i).Major)
					{
						base.get_ByIndex(i).SortOrder += num;
					}
				}
				Resort();
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		private bool AlphaSortHidden()
		{
			bool result = default(bool);
			try
			{
				bool flag;
				int i;
				do
				{
					flag = true;
					int num = base.Count - 1;
					for (i = 1; i <= num; i++)
					{
						clsCharacter clsCharacter = base.get_ByIndex(i);
						clsCharacter cObject = base.get_ByIndex(i + 1);
						if (Operators.CompareString(cObject.Title, clsCharacter.Title, TextCompare: false) < 0)
						{
							flag = false;
							RemoveByID(cObject.ID);
							if (!Inserted(ref cObject, i))
							{
								AddChar(ref cObject);
								clsShared.cLog.AddAction("Error while sorting characters - could not put " + cObject.Title + " back.");
								return result;
							}
						}
					}
				}
				while (!flag);
				i = 0;
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsCharacter clsCharacter = (clsCharacter)enumerator.Current;
						i = (clsCharacter.SortOrder = i + 1);
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}
	}
}
